package com.xn.app.muying.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xn.app.muying.R;
import com.xn.app.muying.bean.HealthRecordsCategoryBean;
import com.xn.app.muying.util.APIMethod;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/4/17.
 */
public class HealthRecordsCategoryAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context context;
    private ArrayList<HealthRecordsCategoryBean> list = new ArrayList();
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;

    public HealthRecordsCategoryAdapter(Context context, ImageLoader loader,
                                        DisplayImageOptions options) {
        super();
        this.context = context;
        this.imageLoader = loader;
        this.options = options;
        inflater = LayoutInflater.from(context);
    }

    public void getData(ArrayList<HealthRecordsCategoryBean> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.healthrecordscategory_item,null);
            holder = new ViewHolder();
            holder.iv = (ImageView) convertView.findViewById(R.id.healthrecordscategory_item_iv);
            holder.title = (TextView) convertView.findViewById(R.id.healthrecordscategory_item_title);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list.get(position).getTestSpeciesImageURL() != null && !list.get(position).getTestSpeciesImageURL().equals("")) {
            //设置图片，如果内存中有，优先从内存中读取
            imageLoader.displayImage(APIMethod.apiURL+list.get(position).getTestSpeciesImageURL(), holder.iv, options);
        } else {
            holder.iv.setImageResource(R.mipmap.load_failed);
        }
        holder.title.setText(list.get(position).getTestSpecies());
        return convertView;
    }
    class ViewHolder {
        ImageView iv;
        TextView title;
    }
}
